<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    /* 
      思路：https://leetcode-cn.com/problems/minimum-path-sum/solution/64-zui-xiao-lu-jing-he-by-alexer-660/
     */
    let grid = [
      [1, 3, 1],
      [1, 5, 1],
      [4, 2, 1]
    ];

    function minPathSum(grid) {
      for (let i = 0; i < grid.length; i++) {
        for (let j = 0; j < grid[0].length; j++) {
          if (i !== 0 && j !== 0)
            grid[i][j] = Math.min(grid[i-1][j], grid[i][j-1]) + grid[i][j];
          else if (i === 0 && j !== 0) 
            grid[i][j] = grid[i][j-1] + grid[i][j];
          else if (i !==0 && j === 0)
            grid[i][j] = grid[i-1][j] + grid[i][j];
          else if (i === 0 && j === 0)
            continue;
        }
      }
      return grid[grid.length-1][grid[0].length-1];
    }
    console.log(minPathSum(grid));
  </script>
</body>
</html>